{
 "metadata": {
  "name": "",
  "signature": "sha256:01206fe491c89e5d487989cb81236cb2b005dc7c0154de6ce39173efe16abed7"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "rd = np.random.randn(100) * 10 \n",
      "x1 = np.linspace(0, 10, 100)\n",
      "y1 = np.random.rand(100) + rd\n",
      "\n",
      "\n",
      "x2 = np.linspace(10, 20, 100)\n",
      "y2 = np.random.rand(100) + rd\n",
      "\n",
      "def plot(x1, y1, x2, y2):\n",
      "    scatter0 = plt.scatter(x1, y1, c='b', marker='o')\n",
      "    scatter1 = plt.scatter(x2, y2, c='r', marker='x')\n",
      "    plt.legend(handles=[scatter0, scatter1], labels=['label0','label1'],loc='best')\n",
      "    \n",
      "plot(x1, y1, x2, y2)\n",
      "plt.show() "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJztnX+MHdWV57+n/SOW+TGNG5LxYrrb\nCREBB2KgRYeFDZOBBAeN4pmI2bBqZs1CZOFkcNvJaoC0ZPesYmlJtICdCRt5h2hJ3Jlk4xkCGk2U\nQMIKRQnMthFkAMfBiX+kLWI8HpoQWQS7++wf9cpdr7p+1/1ZdT5Sqbvq1au6dd9733vuueeeS8wM\nQRAEofn02C6AIAiCYAYRfEEQhJY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]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def knn(K, center, data):\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print (type(x1))\n",
      "x_data = np.hstack((x1,x2))\n",
      "y_data = np.hstack((y1,y2))\n",
      "print (x_data.shape)\n",
      "data = np.hstack((x_data.reshape(-1,1),y_data.reshape(-1,1)))\n",
      "print (data.shape)\n",
      "\n",
      "center = np.array([10, 2])\n",
      "\n",
      "center = np.tile(center,(data.shape[0],1))\n",
      "print (center.shape)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<class 'numpy.ndarray'>\n",
        "(200,)\n",
        "(200, 2)\n",
        "(200, 2)\n"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = np.genfromtxt('testSet.txt')\n",
      "print(len(data))\n",
      "x = data[:20,0]\n",
      "y = data[:20,1]\n",
      "plt.scatter(x,y)\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "80\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAADixJREFUeJzt3f9rZXedx/HXyzSLF+ySHyaLTGZm\nU9glMHSUSCgL84PSFlN1sGFwQRcF8Yf5RaGFbkpn+wdMIYv6g4IM7oJgoeziGJdViSOjP+wPipmm\ndWhrpJS69o6LkSUobMCZ6Xt/SDJf2szk5t7PvZ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D0l6PSLeiIg/S3pB0uOVa+qLiPhdRLy4899/0nbYpR431/YRSZ+Q9M3a\ntZRAsPfJiE/0/QVJP6xdRGFTkn572+e3lDzsJMn2tKRZST+vW0nffVXbN2Fv1y6kBGZQ6kGpib6b\n4l7HGxHf21nnWW3/lH9+kLWhPNvvk/QdSU9GxB9r19Mvtk9J+n1EXLb9kdr1lECw9yAiHt1rue0T\nujXRt3Rrou+Hmjwn7N2Od5ftz0s6JemRyPccbVvS0ds+H9lZlpLtcW2H+vMRcaF2PX12UtInbX9c\n0nsl/aXtb0fEZyvX1TWeYx8A229KmouIYR9gqGu2H5P0ZUkfjoiN2vWUZvs+bXcKP6LtQP+FpH+I\niFeqFtYH3r4b+Zak/42IJ2vXM0g7d+z/GBGnatfSC9rYUcrXJN0v6aLtl2x/o3ZBJe10DH9J0oq2\nOxP/LWOo7zgp6XOSHt75Ll/auZtFQ3DHDgDJcMcOAMkQ7ACQDMEOAMkQ7ACQDMEOAMkQ7ACQDMEO\nAMkQ7ACQzP8Dh9TBfInken8AAAAASUVORK5CYII=\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7f7101849710>"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def compute_distance(centers, data, record):\n",
      "    for i in range(len(data)):\n",
      "        maxValue = float('inf')\n",
      "        index = -1\n",
      "        for j in range(len(centers)):\n",
      "            #D = (centers[j][0]-data[i][0])**2 + (centers[j][1]-data[i][1])**2\n",
      "            #print (D, maxValue)\n",
      "            D = np.sqrt(sum((centers[j]-data[i])**2))\n",
      "            try:\n",
      "                if D < maxValue:\n",
      "                    maxValue = D\n",
      "                    index = j\n",
      "            except ValueError:\n",
      "                print (centers)\n",
      "                print (D, maxValue)\n",
      "                break\n",
      "        record[index][0] += data[i][0]\n",
      "        record[index][1] += data[i][1]\n",
      "        record[index][2] += 1\n",
      "    return record\n",
      "\n",
      "def updata_centers(centers, record):\n",
      "    change = False\n",
      "    for i in range(len(record)):\n",
      "        if record[i][2]:\n",
      "            record[i][0] /= record[i][2]\n",
      "            record[i][1] /= record[i][2]\n",
      "        if centers[i][0] != record[i][0] or centers[i][1] != record[i][1]:\n",
      "            change = True\n",
      "            centers[i][0] = record[i][0] \n",
      "            centers[i][1] = record[i][1]\n",
      "    return change\n",
      "\n",
      "def k_means(k, data, epoches):\n",
      "    len1 = len(data[0])\n",
      "    centers = [[0.0]*len1 for _ in range(k)]\n",
      "    \n",
      "    for i in range(epoches):\n",
      "        record = [[0.0]*(len1+1) for _ in range(k)]\n",
      "        compute_distance(centers, data, record)\n",
      "        isChangge = updata_centers(centers, record)\n",
      "        if not isChangge:\n",
      "            print (\"Cluster complete!\")\n",
      "            break\n",
      "    return centers"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "centers = k_means(4, data, 100)\n",
      "centers"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Cluster complete!\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "[[-3.5397388947368418, -2.893843263157895],\n",
        " [2.6265298999999995, 3.1086801500000001],\n",
        " [-2.4615431499999998, 2.7873755500000006],\n",
        " [2.6507736666666668, -2.790190285714286]]"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "centers = np.array(centers)\n",
      "print (type(centers[:]))\n",
      "\n",
      "plt.scatter(x,y)\n",
      "plt.scatter(centers[:,0],centers[:,1],c='r', marker='x')\n",
      "plt.show"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<class 'numpy.ndarray'>\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "<function matplotlib.pyplot.show>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAD+9JREFUeJzt3VFoZNd9x/Hff2WVDsRlHlYlrHa3\nMrQIFitBZTABPaTYpnKSJRZLCklJIORhXxKQF1dmFb/obRdUshEkEJYkUIjBtGSjlCZB2bDJQwsJ\nGVl2FttRMMZpPJsShSIS6EC0q38fRrMaySPNaO65c+ae+X5ArOZquHPuzuV3zz3n3HPM3QUASMep\n2AUAAIRFsANAYgh2AEgMwQ4AiSHYASAxBDsAJIZgB4DEEOwAkBiCHQAS80iMDz19+rRPTEzE+GgA\nKKz19fXfu/tYp/dFCfaJiQlVq9UYHw0AhWVmv+7mfTTFAEBiCHYASAzBDgCJIdgBIDEEOwAkJsqo\nGABxrG7UtLy2qXvbdZ0pl7QwO6m56fHYxUJgBDswJFY3alq8dVf1nQeSpNp2XYu37koS4Z4YmmKA\nIbG8tvkw1JvqOw+0vLYZqUTIC8EODIl72/UTbUdxEezAkDhTLp1oO4qLYAeGxMLspEqjIwe2lUZH\ntDA7GalEyAudp8CQaHaQMiomfcGC3cxGJFUl1dz9Yqj9AghnbnqcIB8CIZti5iW9GXB/AIAeBAl2\nMzsr6WOSvh5ifwCA3oWqsX9Z0guSdo96g5ldNrOqmVW3trYCfSwA4LDMwW5mFyX9zt3Xj3ufu990\n94q7V8bGOi4AAgDoUYga+4ykj5vZO5JelvSkmX0rwH4BAD3IHOzuvujuZ919QtInJd1x909nLhkA\noCc8oAQAiQn6gJK7/0TST0LuEwBwMtTYASAxBDsAJIZgB4DEEOwAkBiCHQASw7S9A4bFhgFkRbAP\nEBYbBhACwT5AjltsmGDvjLsdoIFgHyAsNtw77naAfXSeD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ee\nF9W4lX+pn2VDeGb2PknflvScu/8hdnnyYmYXJf3O3dfN7O9ilycEgj0Dd3+63XYzm9L+Qt/S/kLf\nTxR5TdijjrfJzD4r6aKkpzy9cbQ1SedaXp/d25YkMxtVI9RfcvdbscuTsxlJHzezj0r6c0l/YWbf\ncvdPRy5XzxjH3gdm9o6kirsP+gRDPTOzZyR9SdKH3X0rdnlCM7NH1OgUfkqNQP+5pH9099ejFiwH\n1qiN/Iuk/3X352KXp5/2auz/5O4XY5clC9rYEcpXJD0q6baZvWpmX4tdoJD2Ooa/IGlNjc7Ef00x\n1PfMSPqMpCf3vstX92qzKAhq7ACQGGrsAJAYgh0AEkOwA0BiCHYASAzBDgCJIdgBIDEEOwAkhmAH\ngMT8PyBB1Xxr97G5AAAAAElFTkSuQmCC\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7f71018926a0>"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def showCluster(data, k, centers, clusterData):\n",
      "    numSamples, dim = data.shape\n",
      "    if dim != 2:\n",
      "        print (\"dimension of your data is not 2\")\n",
      "        return 1\n",
      "    mark = ['or', 'ob', 'og', 'r', '+r', 'sr', 'dr', '<r', 'pr']\n",
      "    if k > len(mark):\n",
      "        print (\"Your k is too large.\")\n",
      "        return 1\n",
      "    \n",
      "    for i in range(numSamples):\n",
      "        markIndex = int(clusterData[i,0])\n",
      "        plt.plot(data[i,0], data[i,1], mark[markIndex])\n",
      "    \n",
      "    mark = ['*r', '*b', '*g', '*k', '+b', 'sb', 'db']\n",
      "    for i in range(k):\n",
      "        plt.plot(centers[i,0], centers[i,1], mark[i], markersize=20)\n",
      "    plot.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}